package com.fengke.cn.yejian11.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.base.BaseActivity;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.impls.UserImpls;
import com.fengke.cn.yejian11.javabean.LoginResult;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class LoginActivity extends BaseActivity {
    private EditText cellphone;
    private Context context=LoginActivity.this;
    private EditText password;
    public static String  tel;
    private String pass;
    private Button login;
    private Button reg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        event();

    }
    public void event()
    {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tel=cellphone.getText().toString();
                pass=password.getText().toString();
                if ("".equals(tel))
                {
                    Toast.makeText(context,"你的手机号码不能为空",Toast.LENGTH_LONG).show();

                }

                else if (pass.equals(""))
                {
                    Toast.makeText(context,"你的登录密码不能为空",Toast.LENGTH_LONG).show();
                }
                else
                {
                    Retrofit re= new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
                    UserImpls service = re.create(UserImpls.class);
                    Call<LoginResult> call = service.getLoginResult(tel, pass);
                    call.enqueue(new Callback<LoginResult>() {
                        @Override
                        public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
                        LoginResult result = response.body();

                       if (result.getError()==null)//error字段为空，登录成功
                        {
                            Toast.makeText(context,""+result.getId(),Toast.LENGTH_SHORT).show();
                            LoginActivity.super.saveUserId(result.getId());
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(context, result.getError(), Toast.LENGTH_LONG);
                        }

                        }

                        @Override
                        public void onFailure(Call<LoginResult> call, Throwable t) {
                            Toast.makeText(context,"登录失败",Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegActivity.class);
                startActivity(intent);
            }
        });
    }
    public void init()
    {
        reg=(Button)findViewById(R.id.Login_reg);
        login =(Button)findViewById(R.id.Login_login);
        cellphone=(EditText) findViewById(R.id.userName);
        password=(EditText) findViewById(R.id.password);

    }
}
